package com.kitty.game.rank.service.handler;

import com.kitty.common.utils.SpringUtils;
import com.kitty.game.enter.FiedValue;
import com.kitty.game.party.model.Party;
import com.kitty.game.party.service.PartyService;
import com.kitty.game.rank.message.RespRankTopUser;
import com.kitty.game.rank.model.PersonInfo;
import com.kitty.game.rank.model.record.PartyCapitalRankRecord;
import com.kitty.game.rank.model.record.RankRecord;
import org.nutz.dao.Cnd;
import org.nutz.dao.Dao;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;

/**帮派资金排行榜处理类*/
@Component
public class PartyCapitalRankHandler extends RankHandler {
    @Override
    public void init() {
        Dao dao = SpringUtils.getBean(Dao.class);
        List<Party> list = dao.query(Party.class, Cnd.NEW().desc("capital"), dao.createPager(1, getRankSize()));
        PartyService partyService = SpringUtils.getBean(PartyService.class);
        for (Party party : list) {
            party.doAfterInit();
            String gid = String.valueOf(party.getId());
            short memberCount = partyService.getMemberCount(party);
            this.update(gid, new PartyCapitalRankRecord(gid, party.getPartyName(), party.getLevel(), memberCount, party.getCapital()));
        }
    }

    /**
     * 获得排行榜返回
     */
    public RespRankTopUser getRankResponse(short type) {
        RespRankTopUser respRankTopUser = new RespRankTopUser();
        respRankTopUser.setType(type);
        respRankTopUser.setCookie(new Long(System.currentTimeMillis() / 1000).intValue());
        List<PersonInfo> personInfoList = getPersonInfoList(getRecordsFrom(0, getRankSize()));
        respRankTopUser.setList(personInfoList);
        respRankTopUser.setSize((short) respRankTopUser.getList().size());

        return respRankTopUser;
    }

    private List<PersonInfo> getPersonInfoList(List<RankRecord> records) {
        List<PersonInfo> list = new ArrayList<>(records.size());
        for (RankRecord rankRecord : records) {
            PartyCapitalRankRecord capitalRankRecord = (PartyCapitalRankRecord)rankRecord;
            PersonInfo personInfo = new PersonInfo();
            personInfo.setList(new ArrayList<>());
            personInfo.getList().add(new FiedValue(305, capitalRankRecord.getGid()));
            personInfo.getList().add(new FiedValue(1, capitalRankRecord.getName()));
            personInfo.getList().add(new FiedValue(31, capitalRankRecord.getLevel()));
            personInfo.getList().add(new FiedValue(856, capitalRankRecord.getMemberCount()));
            personInfo.getList().add(new FiedValue(865, capitalRankRecord.getCapital()));

            list.add(personInfo);
        }
        return list;
    }

    /**
     * 更新排行榜
     */
    public void updateRank(Party party) {
        if (party == null) {return ;}

        String gid = String.valueOf(party.getId());
        PartyService partyService = SpringUtils.getBean(PartyService.class);
        short memberCount = partyService.getMemberCount(party);
        update(gid, new PartyCapitalRankRecord(gid, party.getPartyName(), party.getLevel(), memberCount, party.getCapital()));
    }
}
